package com.hgkj.common.satoken.core;

import cn.dev33.satoken.stp.StpInterface;
import com.hgkj.system.auth.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 自定义权限加载接口实现类
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> perms = new ArrayList<>();
        List<String> roleList = permissionService.getRolesByUserId(Long.parseLong((String) loginId));
        if (roleList.contains("admin")) {
            perms.add("*");
            return perms;
        }
        if (!roleList.isEmpty()) {
            Set<String> permissionList = permissionService.getPermsByRoles(roleList);
            perms.addAll(permissionList);
        }
        return perms;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return permissionService.getRolesByUserId(Long.parseLong((String) loginId));
    }

}
